import React, {Component} from "react";
import {Modal} from "antd";
import InputBox from "../../common/InputBox.js";
import Confirm from "../../common/Confirm.js";
import Relay from "react-relay";
import CreateUserMutation from "./mutation/CreateUserMutation";
import ReactKey from "../../util/ReactKey";
import UpdateUserMutation from "./mutation/UpdateUserMutation";


export default class Addstaff extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            roles: [],
            departname: {},
            partid: 0,
            departmentforchoose: [],
            companyhint: '',
            mal: '',
            connection: '',
            ratepayer: '',
            conname: '',
            title: "",
            uid: "",
            password: ""

        }
        this.key;
        this.Style = {};
        this.roles = [{text: "请选择职务", value: "0"}]
        this.corresponding = {};
        this.correspondingen = {};
        this.pid = {};
        this.i = 0;
        this.j = 0;
        this.k = 0;
        this.isRelay = true;
    }


    state = {visible: false}
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    handleOk = (e) => {
        this.bluron("companyhint");
        this.bluron("connection");
        if (this.isRelay) {
            this.addworker();
        }
    }
    handleCancel = (e) => {
        this.setState({
            visible: false,
        });
    }

    handleClick() {
        //console.warn(111)
    }

    addworker() {
        let companyname1 = this.refs._enterpriseName.state.text;
        let taxpayerNo1 = this.refs._taxpayerNo.state.text;
        let telephone1 = this.refs._telephone.state.text;
        let re_taxpayerNo = /^([0-9]|[a-z]|[A-Z]){6,12}$/;
        let re_telephone = /^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

        var regNull = /(^\s*)|(\s*$)/g;
        if (companyname1.indexOf(" ") >= 0) {
            this.setState({
                companyhint: "姓名不能有空格"
            });
            return;
        }
        if (re_taxpayerNo.test(taxpayerNo1)) {
            this.i = 1;
        } else {
            this.i = 0;
        }
        if (re_telephone.test(telephone1)) {
            this.j = 1;
        } else {
            this.j = 0;
        }
        if (companyname1 != "") {
            this.k = 1;
        } else {
            this.k = 0;
        }
        if (this.state.uid.length != 0) {
            if (this.state.companyhint != "" || this.state.mal != "" || this.state.connection != "" || this.state.ratepayer != "") {
                return;
            }
            Relay.Store.commitUpdate(
                new UpdateUserMutation({
                    uid: this.state.uid,
                    name: this.refs._enterpriseName.state.text,
                    phone: this.refs._telephone.state.text,
                    userId: this.state.uid,
                }),
                {
                    onFailure: () => {
                    },
                    onSuccess: (response) => {
                        let errorcode = response.updateUser.updateUser.errorCode;
                        if (errorcode == 0) {
                            this.setState({
                                visible: false
                            }, () => {
                                this.showInfo("", "编辑用户成功");
                                this.refreshList();
                            });
                        } else {
                            this.showError("error", response.updateUser.updateUser.value);
                        }
                    }
                }
            )
        } else {
            Relay.Store.commitUpdate(
                new CreateUserMutation({
                    name: this.refs._enterpriseName.state.text,
                    phone: this.refs._telephone.state.text,
                    password: this.refs._taxpayerNo.state.text,
                }),
                {
                    onFailure: () => {
                    },
                    onSuccess: (response) => {
                        let errorcode = response.createUser.createUser.errorCode;
                        if (errorcode == 0) {
                            this.setState({
                                visible: false
                            }, () => {
                                this.showInfo("", "创建用户成功");
                                this.refreshList();
                            });
                        } else {
                            this.showError("error", response.createUser.createUser.value);
                        }
                    }
                }
            )
        }
    }

    /**错误提示框*/
    showError(title, value) {
        Modal.error({
            title: !title ? "提示：" : title,
            content: value,
            width: 390
        });
    }

    /*成功提醒*/
    showInfo(title, value) {
        Modal.info({
            title: !title ? "提示：" : title,
            content: value,
            width: 390
        });
    }

    /*初始化数据显示*/
    initHit() {
        this.isRelay = true;
        this.setState({
            companyhint: "",
            connection: "",
            ratepayer: ""
        });
    }

    bluron(type) {
        switch (type) {
            case 'companyhint':
                let companyname1 = this.refs._enterpriseName.state.text;
                // this.companyname = companyname1;
                if (companyname1.indexOf(" ") >= 0) {
                    this.setState({
                        companyhint: "姓名不能有空格"
                    });
                } else if (companyname1 == "") {
                    this.isRelay = false;
                    this.setState({
                        companyhint: "姓名不能为空"
                    });
                } else {
                    this.setState({
                        companyhint: ""
                    });
                }
                return;
            case 'mal':
                let enterpriseCode1 = this.refs._enterpriseCode.state.text;
                let re_enterpriseCode = /^([0-9]|[a-z]|[A-Z]){3,15}@([0-9]|[a-z]|[A-Z]){1,5}[\.]([0-9]|[a-z]|[A-Z]){2,5}$/

                if (re_enterpriseCode.test(enterpriseCode1)) {
                    this.enterpriseCode = enterpriseCode1;
                    this.setState({
                        mal: ""
                    });
                } else if (enterpriseCode1 == "") {
                    this.setState({
                        mal: ""
                    });
                } else {
                    this.isRelay = false;
                    this.setState({
                        mal: "邮箱格式错误，请重新输入"
                    });
                }
                return;

            case 'connection':
                let telephone1 = this.refs._telephone.state.text;
                let re_telephone = /^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

                let telephone;
                if (re_telephone.test(telephone1)) {
                    telephone = telephone1;
                    this.setState({
                        connection: ""
                    });
                } else if (telephone1 == "") {
                    this.isRelay = false;
                    this.setState({
                        connection: "手机号不能为空"
                    });
                } else {
                    this.isRelay = false;
                    this.setState({
                        connection: "手机号格式错误，请重新输入"
                    });
                }
                return;

            case 'ratepayer':
                let taxpayerNo1 = this.refs._taxpayerNo.state.text;
                let re_taxpayerNo = /^([0-9]|[a-z]|[A-Z]){6,12}$/;
                let taxpayerNo;
                if (re_taxpayerNo.test(taxpayerNo1)) {
                    taxpayerNo = taxpayerNo1;
                    this.setState({
                        ratepayer: ""
                    });
                } else if (taxpayerNo1 == "") {
                    this.setState({
                        ratepayer: ""
                    });
                } else {
                    this.isRelay = false;
                    this.setState({
                        ratepayer: "密码格式错误，请重新输入"
                    });
                }
                return;

        }
    }

    /*获取焦点*/
    focuson(type) {
        switch (type) {
            case "companyhint":
                this.isRelay = true;
                this.setState({
                    companyhint: ""
                });
                return;
            case "connection":
                this.isRelay = true;
                this.setState({
                    connection: ""
                });
                return;
            case "ratepayer":
                this.isRelay = true;
                this.setState({
                    ratepayer: ""
                });
                return;
        }
    }

    refreshList() {
        this.props.callbackParentList();
    }

    afterclose() {
        this.key = ReactKey.key
    }


    render() {
        return (
            <Modal className="modal_gre-title"
                  afterClose={this.afterclose.bind(this)} visible={this.state.visible} onOk={this.handleOk}
                   onCancel={this.handleCancel} style={{width: "634px", height: "765px", display: "block"}}
                   title={this.state.title} key={this.key}>
                <span style={{position: "absolute", display: "block", left: 360, top: 13}}>（用于账号登陆）</span>
                <InputBox defaultvalute="请输入手机号" className="addstaff_input_bro_no" titletext="手机号" space="20px"
                          style={{width: 312, height: 30, borderColor: "#efefef", color: "#999", paddingRight: 57}}
                          ref={'_telephone'} maxlength={11} bluron={this.bluron.bind(this, 'connection')}
                          focuson={this.focuson.bind(this, "connection")}/>
                <span style={{
                    color: "#ff6f64",
                    position: "absolute",
                    left: "120px"
                }}>{this.state.connection}</span>
                <InputBox defaultvalute="请输入姓名" className="addstaff_input_bro_no" maxlength={25} titletext="姓名" space="20px" style={{
                    width: 312,
                    height: 30,
                    borderColor: "#efefef",
                    color: "#999",
                    marginTop: "20px",
                    paddingRight: 57
                }} ref={"_enterpriseName"} bluron={this.bluron.bind(this, 'companyhint')}
                          focuson={this.focuson.bind(this, "companyhint")}/>
                <span style={{
                    color: "#ff6f64",
                    position: "absolute",
                    left: "120px",
                }}>{this.state.companyhint}</span>
                <InputBox defaultvalute={this.state.password} className="addstaff_input_bro_no" disabled="true" titletext="密码" space="20px" style={{
                    width: 312,
                    height: 30,
                    borderColor: "#efefef",
                    color: "#999",
                    marginTop: 20,
                    paddingRight: 57
                }} ref={"_taxpayerNo"} bluron={this.bluron.bind(this, 'ratepayer')}
                          focuson={this.focuson.bind(this, "ratepayer")}/>
                <span style={{
                    color: "#ff6f64",
                    position: "absolute",
                    left: "240px",
                    top: "139px"
                }}>{this.state.ratepayer}</span>
                {/*                <Confirm tcontext={this.state.conname} style={{
                 width: 265,
                 height: 43,
                 display: "block",
                 color: "#fff",
                 fontSize: "16px",
                 marginTop: 60,
                 marginLeft: 158
                 }} touch={this.addworker.bind(this)}/>*/}
            </Modal>
        )
    }
}


